(function(factory) {
    if (typeof define === "function" && define.amd)
        define(factory);
    else
        window.cticlient = factory()
}
)(function() {
    var MSGTYPE = $CTI_CONST.CTI_MSG;
    if (!Date.prototype.format)
        Date.prototype.format = function(t) {
            if (t == undefined)
                t = "yyyy-MM-dd HH:mm:ss";
            var date = {
                "M+": this.getMonth() + 1,
                "d+": this.getDate(),
                "h+": this.getHours(),
                "H+": this.getHours(),
                "m+": this.getMinutes(),
                "s+": this.getSeconds(),
                "q+": Math.floor((this.getMonth() + 3) / 3),
                "S+": this.getMilliseconds(),
                w: "\u65e5\u4e00\u4e8c\u4e09\u56db\u4e94\u516d".charAt(this.getDay())
            };
            if (/(y+)/i.test(t))
                t = t.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in date)
                if ((new RegExp("(" + k + ")")).test(t))
                    t = t.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
            return t
        }
        ;
    function addJsonStringValue(jsonobj, name, value) {
        jsonobj[name.toLowerCase()] = {
            type: 3,
            value: value
        }
    }
    function addJsonIntValue(jsonobj, name, value) {
        jsonobj[name.toLowerCase()] = {
            type: 1,
            value: value
        }
    }
    function addJsonDateValue(jsonobj, name, value) {
        jsonobj[name.toLowerCase()] = {
            type: 4,
            value: value
        }
    }
    function addJsonDatasValue(jsonobj, name, value) {
        jsonobj[name.toLowerCase()] = {
            type: 5,
            value: value
        }
    }
    function getJsonString(jsonobj, name) {
        if (jsonobj && jsonobj.hasOwnProperty(name))
            return jsonobj[name];
        return ""
    }
    function isArray(object) {
        return object && typeof object === "object" && typeof object.length === "number" && typeof object.splice === "function" && !object.propertyIsEnumerable("length")
    }
    function arrayfind(arr, findstr, col, colret, nofoundret) {
        if (isArray(arr) && findstr)
            for (var i = 0; i < arr.length; i++)
                if (arr[i][col] == findstr)
                    return arr[i][colret];
        return nofoundret
    }
    var Connection = {
        STATE_IDLE: 0,
        STATE_RINGING: 1,
        STATE_DIALING: 2,
        STATE_TALKING: 3,
        STATE_HELD: 4,
        createNew: function() {
            var connection = {};
            connection.state = Connection.STATE_UNKONW;
            connection.connectionid = "";
            return connection
        }
    };
    var CallInfo = {
        CALLTYPE_UNKNOWN: 0,
        CALLTYPE_INBOUND: 1,
        CALLTYPE_OUTBOUND: 2,
        createNew: function() {
            var callinfo = {};
            callinfo.activecall = null;
            callinfo.heldcall = null;
            callinfo.calltype = CallInfo.CALLTYPE_UNKNOWN;
            callinfo.tpreleased = 0;
            callinfo.starttime = null;
            callinfo.establishtime = null;
            callinfo.endtime = null;
            return callinfo
        }
    };
    var AGENTWORKMODE = {
        AUTOIN: 1,
        MANUALIN: 2,
        AUX: 3,
        ACW: 4
    };
    var ELITECTIClient = {
        createNew: function() {
            var connectionid1 = "conn-1";
            var connectionid2 = "conn-2";
            var auxcodes = [];
            var mysoftphone = {};
            var eventcounter = 0;
            var i_receive = 0;
            var i_request = 0;
            mysoftphone.dn = "";
            mysoftphone.agentid = "";
            mysoftphone.agentpassword = "";
            mysoftphone.agentgroup = "";
            mysoftphone.callinfo = CallInfo.createNew();
            function logmsg() {
                var msg = "[" + c_cti + "]";
                for (var i = 0; i < arguments.length; i++) {
                    var s1 = arguments[i];
                    if (typeof s1 === "object")
                        if (s1.callee) {
                            var args = s1;
                            var msg1 = "";
                            for (var j = 0; j < args.length; j++) {
                                s1 = args[j];
                                if (typeof s1 === "object")
                                    s1 = JSON.stringify(s1);
                                if (j === 0)
                                    msg1 = s1;
                                else if ([" ", ",", ":"].indexOf(msg1.substr(msg1.length - 1)) >= 0)
                                    msg1 = msg1 + s1;
                                else
                                    msg1 = msg1 + "," + s1
                            }
                            s1 = msg1
                        } else
                            s1 = JSON.stringify(s1);
                    else if (typeof s1 === "function")
                        continue;
                    if (i === 0)
                        msg = msg + s1;
                    else if ([" ", ",", ":"].indexOf(msg.substr(msg.length - 1)) >= 0)
                        msg = msg + s1;
                    else
                        msg = msg + "," + s1
                }
                $SC.addconsolelog(msg)
            }
            function logreceive() {
                i_receive++;
                logmsg("receive ", i_receive, arguments)
            }
            function logret() {
                logmsg("return ", arguments)
            }
            function logreq() {
                i_request++;
                logmsg("request ", i_request, arguments)
            }
            function dumpcallinfo(callinfo, sfor) {
                logmsg("dumpcallinfo " + sfor);
                if (callinfo.activecall)
                    logmsg("activecall,connid:" + callinfo.activecall.connectionid + ",state:" + callinfo.activecall.state + ",callid:" + callinfo.activecall.callid);
                else
                    logmsg("activecall:null");
                if (callinfo.heldcall)
                    logmsg("heldcall,connid:" + callinfo.heldcall.connectionid + ",state:" + callinfo.heldcall.state + ",callid:" + callinfo.heldcall.callid);
                else
                    logmsg("heldcall:null")
            }
            function returnbyresult(data, obj) {
                if (data && data.code)
                    if (data.code == "0")
                        return true;
                    else {
                        if (obj)
                            obj.InternalOnErrorCode(data.code, data.message);
                        return false
                    }
                else
                    return true
            }
            mysoftphone.getconfig = function(configname, options) {
                var ret = $SC._getconfig(c_cti + "config", configname, mysoftphone.agentid, mysoftphone.dn) || Config[configname];
                if (ret && options && options.type == "object" && typeof ret == "string")
                    try {
                        ret = eval(ret)
                    } catch (err) {}
                return ret
            }
            ;
            mysoftphone.setconfig = function(config) {
                logmsg("setconfig", config);
                for (var v in config)
                    Config[v] = config[v]
            }
            ;
            mysoftphone.onctievent = function(evt) {
                if (!evt.servertime)
                    addJsonDateValue(evt, "servertime", $SC.getservertime());
                logmsg("[OnCTIEvent]:" + JSON.stringify(evt));
                this.w_onMessage(evt)
            }
            ;
            mysoftphone.InternalOnErrorCode = function(errorCode, errorMsg) {
                var evt = {};
                evt.event = {
                    type: 1,
                    value: $CTI_CONST.CTI_MSG.EVT_ERROR
                };
                evt.dn = {
                    type: 3,
                    value: this.dn
                };
                if (!errorCode)
                    errorCode = 80001;
                evt.errorcode = {
                    type: 3,
                    value: errorCode
                };
                evt.errordesc = {
                    type: 3,
                    value: errorMsg
                };
                mysoftphone.onctievent(evt)
            }
            ;
            mysoftphone.getdesttype = function(dest) {
                return dest
            }
            ;
            mysoftphone.keepalive = function() {
                logmsg("no keepalive")
            }
            ;
            window.m7Phone = null;
            window.onsdkevent = function(type, data) {
                logreceive(type, data);
                switch (type) {
                case "EvtLogon":
                    mysoftphone.OnAgentLogin(data);
                    break;
                case "EvtPeerStatusChanged":
                    if (data == "0")
                        mysoftphone.OnAgentReady(data);
                    else
                        mysoftphone.OnAgentNotReady(data);
                    break;
                case "unload":
                    mysoftphone.OnLinkDisconnected();
                    break;
                case "EvtDialing":
                    mysoftphone.OnDialing(data);
                    break;
                case "EvtRing":
                    mysoftphone.OnRinging(data);
                    break;
                case "EvtConnected":
                    mysoftphone.OnEstablished(data);
                    break;
                case "EvtHangup":
                    mysoftphone.OnReleased(data);
                    break;
                case "EvtBarChange":
                    if (data == "Local_stHold")
                        mysoftphone.OnHeld(data);
                    if (data == "Local_stTalking")
                        mysoftphone.OnRetrieved(data);
                    break;
                default:
                    logmsg("untreated event", type)
                }
            }
            ;
            mysoftphone.connectserver = function(serverendpoint, onOpen, onClose, onMessage, onError, onSendMessage, agentId, dn, agentPassword, agentGroup) {
                mysoftphone.w_onSendMessage = onSendMessage;
                mysoftphone.w_onMessage = onMessage;
                mysoftphone.w_onError = onError;
                mysoftphone.w_onOpen = onOpen;
                mysoftphone.w_onClose = onClose;
                mysoftphone.agentid = agentId;
                mysoftphone.dn = dn;
                var auxcode = $SC.executeselectsql("softphone.auxcode.query.v201610", []);
                if (auxcode && auxcode.value)
                    auxcodes = auxcode.value[0].fields;
                else
                    auxcodes = [];
                mysoftphone.OnLinkConnected()
            }
            ;
            mysoftphone.registerdn = function(dn) {
                this.dn = dn;
                this.channelid = 1;
                mysoftphone.OnRegistered()
            }
            ;
            mysoftphone.login = function(agentid, dn, agentpassword, agentgroup, workmode, acdagent) {
                mysoftphone.agentid = agentid;
                mysoftphone.dn = dn;
                var loginType = mysoftphone.getphonetype(dn, agentid);
                var loginStatus = "1";
                logreq("login", agentid, loginType);
                document.getElementById("edb").src = "edb/html/eliteproxymain.html?loginName=" + agentid + "&password=" + encodeURIComponent(agentpassword) + "&loginType=" + loginType + "&agentStatus=" + loginStatus
            }
            ;
            mysoftphone.agentlogout = function() {
                logreq("m7Phone.m7exit");
                m7Phone.m7exit()
            }
            ;
            mysoftphone.agentnotready = function(workmode) {
                logreq("m7Phone.m7SetBusy", true, "1");
                m7Phone.m7SetBusy(true, "1")
            }
            ;
            mysoftphone.aux = function(reasoncode) {
                logreq("m7Phone.m7SetBusy", true, "2");
                m7Phone.m7SetBusy(true, "2")
            }
            ;
            mysoftphone.acw = function() {
                logreq("unsupported request " + "acw")
            }
            ;
            mysoftphone.agentready = function(workmode) {
                logreq("m7Phone.m7SetBusy", false, "0");
                m7Phone.m7SetBusy(false, "0")
            }
            ;
            mysoftphone.makecall = function(dest, userdatas) {
                logreq("m7Phone.dialout", dest);
                m7Phone.dialout(dest)
            }
            ;
            mysoftphone.releasecall = function() {
                logreq("m7Phone.hangup");
                m7Phone.hangup()
            }
            ;
            mysoftphone.holdcall = function() {
                logreq("m7Phone.hold");
                m7Phone.hold()
            }
            ;
            mysoftphone.retrievecall = function(callid) {
                logreq("m7Phone.unhold");
                m7Phone.unhold()
            }
            ;
            mysoftphone.mute = function() {
                logmsg("TBD mute")
            }
            ;
            mysoftphone.unmute = function() {
                logmsg("TBD unmute")
            }
            ;
            mysoftphone.answercall = function() {
                logreq("m7Phone.answer");
                m7Phone.answer()
            }
            ;
            mysoftphone.consult = function(dest, userdatas) {
                logmsg("TBD", "consult")
            }
            ;
            mysoftphone.inittransfer = function(callid, dest, userdatas) {
                logmsg("TBD", "inittransfer")
            }
            ;
            mysoftphone.initconference = function(callid, dest, userdatas) {
                logmsg("TBD", "initconference")
            }
            ;
            mysoftphone.reconnect = function() {
                logmsg("TBD", "reconnect")
            }
            ;
            mysoftphone.completetransfer = function() {
                logmsg("TBD", "completetransfer")
            }
            ;
            mysoftphone.completeconference = function() {
                logmsg("TBD", "completeconference")
            }
            ;
            mysoftphone.mutetransfer = function(callid, dest, userdatas) {
                logmsg("TBD", "mutetransfer")
            }
            ;
            mysoftphone.senddtmf = function(dtmf) {
                logreq("m7Phone.playDtmf", dtmf);
                m7Phone.playDtmf(dtmf)
            }
            ;
            mysoftphone.listen = function(dn, agentid, callback) {
                logmsg("TBD", "listen")
            }
            ;
            mysoftphone.bargein = function(dn, agentid, callback) {
                logmsg("TBD", "bargein")
            }
            ;
            mysoftphone.intercept = function(dn, agentid, callback) {
                logmsg("TBD", "intercept")
            }
            ;
            mysoftphone.forcedisconnect = function(dn, agentid, callback) {
                logmsg("TBD", "forcedisconnect")
            }
            ;
            mysoftphone.forceagentlogout = function(dest) {
                logmsg("TBD", "forceagentlogout")
            }
            ;
            mysoftphone.forceagentready = function(dest) {
                logmsg("TBD", "forceagentready")
            }
            ;
            mysoftphone.forceagentnotready = function(dest) {
                logmsg("Ignored Request forceagentnotready", dest, "invalid dest")
            }
            ;
            mysoftphone.getphonetype = function(phone, agentid) {
                console.log($SC._getconfig("CTI", "m7logintype", agentid, phone));
                return $SC._getconfig("CTI", "m7logintype", agentid, phone);
                if (Config.sipphoneregex && Config.sipphoneregex.test(phoneno))
                    return "SIP";
                else if (Config.webrtcregex && Config.webrtcregex.test(phoneno))
                    return "WEBRTC";
                else if (Config.softphoneregex && Config.softphoneregex.test(phoneno))
                    return "PSTN";
                else
                    return Config.defaultphonetype || "WEBRTC"
            }
            ;
            mysoftphone.OnLinkConnected = function() {
                var evt = {};
                addJsonIntValue(evt, "event", MSGTYPE.EVT_LINKCONNCTED);
                addJsonStringValue(evt, "eventdesc", "EVT_LINKCONNECTED");
                mysoftphone.onctievent(evt)
            }
            ;
            mysoftphone.OnLinkDisconnected = function(data) {
                var evt = {};
                addJsonIntValue(evt, "event", MSGTYPE.EVT_LINKDISCONNCTED);
                addJsonStringValue(evt, "eventdesc", "EVT_LINKDISCONNCTED");
                mysoftphone.onctievent(evt)
            }
            ;
            mysoftphone.OnRegistered = function() {
                var ret = {};
                addJsonIntValue(ret, "dn", this.dn);
                addJsonIntValue(ret, "channelid", this.channelid);
                addJsonIntValue(ret, "event", MSGTYPE.EVT_REGISTERED);
                addJsonStringValue(ret, "eventdesc", "EVT_REGISTERED");
                mysoftphone.onctievent(ret)
            }
            ;
            mysoftphone.OnRingback = function(data) {
                logmsg("\u6b64\u5904\u5e94\u53d1\u9001ringback\u6d88\u606f", data)
            }
            ;
            mysoftphone.OnDataUpdated = function(data) {
                var evt = {};
                addJsonIntValue(evt, "event", MSGTYPE.EVT_DATAUPDATED);
                addJsonStringValue(evt, "eventdesc", "EVT_DATAUPDATED");
                mysoftphone.onctievent(evt)
            }
            ;
            mysoftphone.OnReleased = function(data) {
                var servertime;
                var evt = {};
                if (this.callinfo.activecall != null) {
                    console.info("\u6b64\u5904\u5e94\u53d1\u9001released\u6d88\u606f for activecall,connectionid:" + this.callinfo.activecall.connectionid);
                    servertime = $SC.getservertime();
                    addJsonIntValue(evt, "event", MSGTYPE.EVT_RELEASED);
                    addJsonStringValue(evt, "eventdesc", "EVT_RELEASED");
                    addJsonDateValue(evt, "servertime", servertime);
                    addJsonStringValue(evt, "dn", mysoftphone.dn);
                    addJsonStringValue(evt, "callid", this.callinfo.activecall.connectionid);
                    addJsonStringValue(evt, "channelid", mysoftphone.channelid);
                    this.callinfo.activecall = null;
                    mysoftphone.onctievent(evt)
                } else if (this.callinfo.heldcall != null) {
                    console.info("\u6b64\u5904\u5e94\u53d1\u9001released\u6d88\u606f for heldcall,connectionid:" + this.callinfo.heldcall.connectionid);
                    servertime = $SC.getservertime();
                    addJsonIntValue(evt, "event", MSGTYPE.EVT_RELEASED);
                    addJsonStringValue(evt, "eventdesc", "EVT_RELEASED");
                    addJsonDateValue(evt, "servertime", servertime);
                    addJsonStringValue(evt, "dn", mysoftphone.dn);
                    addJsonStringValue(evt, "callid", this.callinfo.heldcall.connectionid);
                    addJsonStringValue(evt, "channelid", mysoftphone.channelid);
                    this.callinfo.heldcall = null;
                    mysoftphone.onctievent(evt)
                }
                dumpcallinfo(this.callinfo, "OnReleased");
                mysoftphone.OnRecordingStarted(data, servertime);
                if (this.callinfo.activecall || this.callinfo.heldcall)
                    ;
                else
                    mysoftphone.OnAgentACW()
            }
            ;
            mysoftphone.OnEstablished = function(data) {
                logmsg("OnEstablished");
                if (1) {
                    var servertime = $SC.getservertime();
                    this.callinfo.activecall.state = Connection.STATE_TALKING;
                    if (this.callinfo.establishtime == undefined)
                        this.callinfo.establishtime = servertime;
                    var evt = {};
                    addJsonIntValue(evt, "event", MSGTYPE.EVT_ESTABLISH);
                    addJsonStringValue(evt, "eventdesc", "EVT_ESTABLISH");
                    addJsonDateValue(evt, "servertime", servertime);
                    addJsonStringValue(evt, "dn", mysoftphone.dn);
                    addJsonStringValue(evt, "channelid", mysoftphone.channelid);
                    var ucid = getJsonString(data, "callSheetId");
                    if (ucid) {
                        var userdatas = {};
                        addJsonStringValue(userdatas, "ucid", ucid);
                        addJsonDatasValue(evt, "userdata", userdatas)
                    }
                    addJsonStringValue(evt, "callid", this.callinfo.activecall.connectionid);
                    if (this.callinfo.heldcall)
                        addJsonStringValue(evt, "prevconnectionid", this.callinfo.heldcall.connectionid);
                    console.info("\u6b64\u5904\u5e94\u53d1\u9001Establish\u6d88\u606f");
                    mysoftphone.onctievent(evt)
                }
                dumpcallinfo(this.callinfo, "OnEstablished")
            }
            ;
            mysoftphone.OnConferenced = function(data) {
                if (this.callinfo.heldcall != null && this.callinfo.activecall != null) {
                    var evt = {};
                    addJsonIntValue(evt, "event", MSGTYPE.EVT_CONFERENCED);
                    addJsonIntValue(evt, "eventdesc", "EVT_CONFERENCED");
                    addJsonStringValue(evt, "dn", mysoftphone.dn);
                    addJsonStringValue(evt, "channelid", mysoftphone.channelid);
                    addJsonStringValue(evt, "callid", this.callinfo.activecall.connectionid);
                    addJsonStringValue(evt, "prevconnectionid", this.callinfo.heldcall.connectionid);
                    var addedParty;
                    if (addedParty)
                        addJsonStringValue(evt, "addedparty", addedParty);
                    this.callinfo.activecall.state = Connection.STATE_TALKING;
                    this.callinfo.heldcall = null;
                    mysoftphone.onctievent(evt)
                }
                dumpcallinfo(this.callinfo, "OnConferenced")
            }
            ;
            mysoftphone.OnHeld = function(data) {
                if (this.callinfo.activecall != null)
                    if (this.callinfo.heldcall == null) {
                        console.info("\u6b64\u5904\u53d1\u9001EVT_HELD");
                        this.callinfo.heldcall = this.callinfo.activecall;
                        this.callinfo.heldcall.state = Connection.STATE_HELD;
                        this.callinfo.activecall = null;
                        var evt = {};
                        addJsonIntValue(evt, "event", MSGTYPE.EVT_HELD);
                        addJsonIntValue(evt, "eventdesc", "EVT_HELD");
                        addJsonStringValue(evt, "dn", mysoftphone.dn);
                        addJsonStringValue(evt, "channelid", mysoftphone.channelid);
                        addJsonStringValue(evt, "holdingdevice", mysoftphone.dn);
                        addJsonStringValue(evt, "callid", this.callinfo.heldcall.connectionid);
                        mysoftphone.onctievent(evt)
                    } else
                        ;dumpcallinfo(this.callinfo, "OnHeld")
            }
            ;
            mysoftphone.OnRetrieved = function(data) {
                if (this.callinfo.heldcall != null) {
                    this.callinfo.activecall = this.callinfo.heldcall;
                    this.callinfo.activecall.state = Connection.STATE_TALKING;
                    this.callinfo.heldcall = null;
                    var evt = {};
                    addJsonIntValue(evt, "event", MSGTYPE.EVT_RETRIEVED);
                    addJsonIntValue(evt, "eventdesc", "EVT_RETRIEVED");
                    addJsonStringValue(evt, "dn", mysoftphone.dn);
                    addJsonStringValue(evt, "channelid", mysoftphone.channelid);
                    addJsonStringValue(evt, "callid", this.callinfo.activecall.connectionid);
                    mysoftphone.onctievent(evt)
                }
            }
            ;
            mysoftphone.OnMute = function(data) {
                var evt = {};
                addJsonIntValue(evt, "event", MSGTYPE.EVT_MUTED);
                addJsonStringValue(evt, "eventdesc", "EVT_MUTED");
                mysoftphone.onctievent(evt)
            }
            ;
            mysoftphone.OnUnMute = function(data) {
                var evt = {};
                addJsonIntValue(evt, "event", MSGTYPE.EVT_UNMUTED);
                addJsonStringValue(evt, "eventdesc", "EVT_UNMUTED");
                mysoftphone.onctievent(evt)
            }
            ;
            mysoftphone.OnRecordingStarted = function(data, stime) {
                var servertime = stime;
                if (!servertime)
                    servertime = $SC.getservertime();
                if (data && data.data && data.data.MonitorFilename) {
                    var evt = {};
                    addJsonIntValue(evt, "event", MSGTYPE.EVT_RECORDSTARTED);
                    addJsonStringValue(evt, "eventdesc", "EVT_RECORDSTARTED");
                    addJsonStringValue(evt, "dn", mysoftphone.dn);
                    addJsonStringValue(evt, "channelid", mysoftphone.channelid);
                    if (servertime)
                        addJsonDateValue(evt, "servertime", servertime);
                    var recordfilename = getJsonString(data.data, "MonitorFilename");
                    var beginTime = getJsonString(data, "beginTime");
                    var endTime = getJsonString(data, "endTime");
                    var userdatas = {};
                    addJsonStringValue(userdatas, "recordfilename", recordfilename);
                    try {
                        if (beginTime && endTime) {
                            var reclen = (new Date(endTime)).valueOf() - (new Date(beginTime)).valueOf();
                            if (reclen > 0) {
                                var recstarttime = (new Date((new Date(servertime)).valueOf() - reclen)).format("yyyy-MM-dd hh:mm:ss");
                                addJsonDateValue(userdatas, "begintime", recstarttime);
                                addJsonDateValue(userdatas, "endtime", servertime);
                                addJsonDatasValue(evt, "userdata", userdatas)
                            }
                        }
                    } catch (err) {
                        logmsg("unexpected beginTime or endTime")
                    }
                    mysoftphone.onctievent(evt)
                }
            }
            ;
            mysoftphone.OnAllCallReleased = function() {
                var servertime;
                var evt = {};
                if (this.callinfo.activecall != null) {
                    servertime = $SC.getservertime();
                    addJsonIntValue(evt, "event", MSGTYPE.EVT_RELEASED);
                    addJsonStringValue(evt, "eventdesc", "EVT_RELEASED");
                    addJsonDateValue(evt, "servertime", servertime);
                    addJsonStringValue(evt, "dn", mysoftphone.dn);
                    addJsonStringValue(evt, "callid", this.callinfo.activecall.connectionid);
                    addJsonStringValue(evt, "channelid", mysoftphone.channelid);
                    this.callinfo.activecall = null;
                    mysoftphone.onctievent(evt)
                }
                if (this.callinfo.heldcall != null) {
                    servertime = $SC.getservertime();
                    addJsonIntValue(evt, "event", MSGTYPE.EVT_RELEASED);
                    addJsonStringValue(evt, "eventdesc", "EVT_RELEASED");
                    addJsonDateValue(evt, "servertime", servertime);
                    addJsonStringValue(evt, "dn", mysoftphone.dn);
                    addJsonStringValue(evt, "callid", this.callinfo.heldcall.connectionid);
                    addJsonStringValue(evt, "channelid", mysoftphone.channelid);
                    this.callinfo.heldcall = null;
                    mysoftphone.onctievent(evt)
                }
            }
            ;
            mysoftphone.OnAgentLogin = function(data) {
                var evt = {};
                addJsonIntValue(evt, "event", MSGTYPE.EVT_AGENTLOGIN);
                addJsonStringValue(evt, "eventdesc", "EVT_AGENTLOGIN");
                addJsonIntValue(evt, "WorkMode", 3);
                addJsonStringValue(evt, "dn", mysoftphone.dn);
                addJsonStringValue(evt, "channelid", mysoftphone.channelid);
                mysoftphone.onctievent(evt)
            }
            ;
            mysoftphone.OnAgentLogout = function(data) {
                mysoftphone.logoutrequest = 0;
                var evt = {};
                addJsonIntValue(evt, "event", MSGTYPE.EVT_AGENTLOGOUT);
                addJsonStringValue(evt, "eventdesc", "EVT_AGENTLOGOUT");
                if (event && event.status)
                    addJsonStringValue(evt, "status", event.status);
                addJsonIntValue(evt, "WorkMode", 0);
                addJsonStringValue(evt, "dn", mysoftphone.dn);
                addJsonStringValue(evt, "channelid", mysoftphone.channelid);
                mysoftphone.onctievent(evt)
            }
            ;
            mysoftphone.OnAgentNotReady = function(data) {
                if (event == "99")
                    return mysoftphone.OnAgentACW(data);
                var reasoncode = "";
                if (event == "2")
                    reasoncode = "2";
                else
                    reasoncode = "";
                var evt = {};
                addJsonIntValue(evt, "event", MSGTYPE.EVT_AGENTNOTREADY);
                addJsonStringValue(evt, "eventdesc", "EVT_AGENTNOTREADY");
                addJsonIntValue(evt, "WorkMode", 3);
                if (reasoncode) {
                    addJsonStringValue(evt, "reasoncode", reasoncode);
                    addJsonStringValue(evt, "reasonname", reasonname)
                }
                addJsonStringValue(evt, "dn", mysoftphone.dn);
                addJsonStringValue(evt, "channelid", mysoftphone.channelid);
                mysoftphone.onctievent(evt)
            }
            ;
            mysoftphone.OnAgentACW = function(data) {
                var evt = {};
                addJsonIntValue(evt, "event", MSGTYPE.EVT_AGENTNOTREADY);
                addJsonStringValue(evt, "eventdesc", "EVT_AGENTNOTREADY");
                addJsonIntValue(evt, "WorkMode", 4);
                addJsonStringValue(evt, "dn", mysoftphone.dn);
                addJsonStringValue(evt, "channelid", mysoftphone.channelid);
                mysoftphone.onctievent(evt)
            }
            ;
            mysoftphone.OnAgentReady = function(data) {
                var evt = {};
                addJsonIntValue(evt, "event", MSGTYPE.EVT_AGENTREADY);
                addJsonStringValue(evt, "eventdesc", "EVT_AGENTREADY");
                addJsonIntValue(evt, "workmode", 2);
                addJsonStringValue(evt, "dn", mysoftphone.dn);
                addJsonStringValue(evt, "channelid", mysoftphone.channelid);
                mysoftphone.onctievent(evt)
            }
            ;
            mysoftphone.OnRinging = function(data) {
                logmsg("OnRinging");
                if (this.callinfo == null)
                    this.callinfo = CallInfo.createNew();
                if (this.callinfo.activecall == null) {
                    this.callinfo.activecall = Connection.createNew();
                    this.callinfo.starttime = $SC.getservertime();
                    this.callinfo.activecall.state = Connection.STATE_RINGING;
                    this.callinfo.activecall.connectionid = connectionid1;
                    if (data) {
                        this.callinfo.ani = data.originCallNo;
                        this.callinfo.dnis = data.originCalledNo;
                        this.callinfo.activecall.callid = data.callSheetId
                    }
                    this.callinfo.calltype = CallInfo.CALLTYPE_INBOUND;
                    console.info("\u6b64\u5904\u5e94\u53d1\u9001ringing\u6d88\u606f");
                    dumpcallinfo(this.callinfo, "OnRinging");
                    var evt = {};
                    addJsonIntValue(evt, "event", MSGTYPE.EVT_RINGING);
                    addJsonIntValue(evt, "eventdesc", "EVT_RINGING");
                    addJsonStringValue(evt, "dn", mysoftphone.dn);
                    addJsonStringValue(evt, "callid", this.callinfo.activecall.connectionid);
                    addJsonStringValue(evt, "channelid", mysoftphone.channelid);
                    addJsonStringValue(evt, "dnis", this.callinfo.dnis);
                    addJsonStringValue(evt, "ani", this.callinfo.ani);
                    var userdatas = {};
                    var ucid = this.callinfo.activecall.callid;
                    if (ucid)
                        addJsonStringValue(userdatas, "ucid", ucid);
                    addJsonDatasValue(evt, "userdata", userdatas);
                    mysoftphone.onctievent(evt)
                } else
                    console.warn("\u6b64\u65f6\u6709activecall")
            }
            ;
            mysoftphone.OnDialing = function(data) {
                logmsg("OnDialing");
                if (this.callinfo.activecall == null && this.callinfo.heldcall == null)
                    this.callinfo = CallInfo.createNew();
                if (this.callinfo.activecall != null && this.callinfo.heldcall == null) {
                    logmsg("OnHeld before OnDialing");
                    this.OnHeld()
                }
                if (!this.callinfo.activecall) {
                    this.callinfo.activecall = Connection.createNew();
                    this.callinfo.starttime = $SC.getservertime();
                    this.callinfo.activecall.state = Connection.STATE_DIALING;
                    this.callinfo.activecall.connectionid = connectionid1;
                    if (this.callinfo.heldcall == null) {
                        this.callinfo.originCallNo = getJsonString(data, "originCallNo");
                        this.callinfo.originCalledNo = getJsonString(data.data, "originCalledNo");
                        this.callinfo.ani = mysoftphone.agentid;
                        if (data && data.data)
                            this.callinfo.dnis = getJsonString(data.data, "ListenExten");
                        if (!this.callinfo.dnis)
                            this.callinfo.dnis = this.callinfo.originCalledNo;
                        this.callinfo.calltype = CallInfo.CALLTYPE_OUTBOUND
                    } else if (this.callinfo.heldcall.connectionid === connectionid1)
                        this.callinfo.activecall.connectionid = connectionid2;
                    this.callinfo.activecall.callid = getJsonString(data, "callSheetId");
                    console.info("\u6b64\u5904\u5e94\u53d1\u9001dialing\u6d88\u606f");
                    dumpcallinfo(this.callinfo, "OnDialing");
                    var evt = {};
                    addJsonIntValue(evt, "event", MSGTYPE.EVT_DIALING);
                    addJsonIntValue(evt, "eventdesc", "EVT_DIALING");
                    addJsonStringValue(evt, "dn", mysoftphone.dn);
                    addJsonStringValue(evt, "callid", this.callinfo.activecall.connectionid);
                    addJsonStringValue(evt, "channelid", mysoftphone.channelid);
                    addJsonStringValue(evt, "dnis", this.callinfo.dnis);
                    addJsonStringValue(evt, "ani", this.callinfo.ani);
                    if (this.callinfo.heldcall)
                        addJsonStringValue(evt, "prevconnectionid", this.callinfo.heldcall.connectionid);
                    var userdatas = {};
                    if (this.callinfo.activecall.callid)
                        addJsonStringValue(userdatas, "ucid", this.callinfo.activecall.callid);
                    if (this.callinfo.originCallNo)
                        addJsonStringValue(userdatas, "origincallno", this.callinfo.originCallNo);
                    addJsonDatasValue(evt, "userdata", userdatas);
                    mysoftphone.onctievent(evt)
                }
            }
            ;
            return mysoftphone
        }
    };
    return ELITECTIClient.createNew()
});